/**
 * 用途         :音乐播放模块
 * 开发者       :郑品延
 * 修改时间     :2019/10/24
 */

export default class SoundUtil{
    public static bgmName = '';         //当前背景音乐的名字
    public static bgmId = 0;            //当前背景音乐的ID
    public static musicKey = null;
    public static effectKey = null;

    public static loopEffect = {};      //保存循环播放的音效id

    public static init(){
        Log('初始化音乐变量')
        SoundUtil.musicKey = ReadLocal('SoundSwitch', 'true') == 'true';
        SoundUtil.effectKey = ReadLocal('EffectSwitch', 'true') == 'true';
        cc.audioEngine.setEffectsVolume(1);
    }
    /**
     * 播放背景音乐,使用前请调用SoundUtil.init();
     * @param fileName  背景音乐名称,在Sound文件夹里
     * @param isLoop    是否循环播放
     */
    public static playMusic(fileName,isLoop = true){
        if (SoundUtil.musicKey == null){
            SoundUtil.init();
        }
        if (SoundUtil.musicKey == false) {
            Log('当前是静音模式')
            SoundUtil.bgmName = fileName;
            return;
        }
        if (fileName == SoundUtil.bgmName){
            return;
        }
        if (!fileName){
            fileName = SoundUtil.bgmName;
        }
        SoundUtil.stopMusic();
        var url = 'Sound/' + fileName;
        cc.loader.loadRes(url, cc.AudioClip,  (err, res) => {
            if (err) {
                Log('加载本地音乐资源失败: ' + err);
                return;
            }
            Log('播放音乐 = ',fileName)
            SoundUtil.bgmId = cc.audioEngine.play(res, isLoop,1);
            SoundUtil.bgmName = fileName;
        });
    }
    /**
     * 停止背景音乐播放 ,使用前请调用SoundUtil.init();
     * @param id 如果没有传ID 则用内存中背景音乐的ID
     */
    public static stopMusic(id?){
        let audioId = id || SoundUtil.bgmId;
        cc.audioEngine.stop(audioId);
        SoundUtil.bgmId = -1;
    }
    /**
     * 暂停背景音乐
     */
    public static pauseMusic(){
        cc.audioEngine.pause(SoundUtil.bgmId);
    }
    /**
     * 恢复背景音乐
     */
    public static resumeMusic(){
        cc.audioEngine.resume(SoundUtil.bgmId);
    }
    /**
     * 播放音效,使用前请调用SoundUtil.init();
     * @param fileName 音效名称 Sound文件夹下
     * @param isLoop 是否循环播放
     */
    public static playEffect(fileName, isLoop = false) {
        if (SoundUtil.effectKey == null){
            SoundUtil.init();
        }
        if (SoundUtil.effectKey == false) {
            if (isLoop && ( !SoundUtil.loopEffect.hasOwnProperty(fileName)|| SoundUtil.loopEffect[fileName] == null)){
                SoundUtil.loopEffect[fileName] = 1;
            }
            return;
        }
        let url = 'Sound/' + fileName;
        if (isLoop && SoundUtil.loopEffect.hasOwnProperty(fileName) && SoundUtil.loopEffect[fileName] != null){
            Log('已经在播了,就不打断了')
            return;
        }
        cc.loader.loadRes(url, (err, res) => {
            if (err) {
                Log(err.message || err);
                return;
            }
            Log('播放音效:',fileName);
            //如果是循环播放的音效需要做重复处理
            if (!isLoop){
                let audioId = cc.audioEngine.playEffect(res, isLoop);
                return;
            }
            if (isLoop && SoundUtil.loopEffect.hasOwnProperty(fileName) && SoundUtil.loopEffect[fileName] != null){
                Log('已经在播了,就不打断了')
                return;
            }
            let audioId = cc.audioEngine.playEffect(res, isLoop);
            SoundUtil.loopEffect[fileName] = audioId;
        });
    }
    /**
     * 停止循环播放的音效
     * @param name 音效名称
     */
    public static stopLoopEffectByName(name){
        // Log('停止循环音效:',name)
        // Log(SoundUtil.loopEffect)
        if (SoundUtil.loopEffect.hasOwnProperty(name) && SoundUtil.loopEffect[name] != null){
            let oldId = SoundUtil.loopEffect[name];
            cc.audioEngine.stopEffect(oldId);
            delete SoundUtil.loopEffect[name];
        }
    }

    /**
     * 停止所有音效
     */
    public static stopAllEffect(){
        cc.audioEngine.stopAllEffects();
        SoundUtil.loopEffect = {};
    }

    /**
     * 转换音乐开关功能,使用前请调用SoundUtil.init();
     */
    public static SetMusicSwitch(){
        let key = ReadLocal('SoundSwitch', 'true');
        if (key == 'true') {
            SaveLocal('SoundSwitch', 'false');
            SoundUtil.musicKey = false;
            SoundUtil.stopMusic();
        } else {
            SaveLocal('SoundSwitch', 'true');
            SoundUtil.musicKey = true;
            SoundUtil.playMusic(null);
        }
    }
    /**
     * 转换音乐开关功能,使用前请调用SoundUtil.init();
     */
    public static SetEffectSwitch(){
        let key = ReadLocal('EffectSwitch', 'true');
        if (key == 'true') {
            SaveLocal('EffectSwitch', 'false');
            SoundUtil.effectKey = false;
            SoundUtil.stopAllEffect();
        } else {
            SaveLocal('EffectSwitch', 'true');
            SoundUtil.effectKey = true;
        } 
    }
    /**
     * 转换音乐开关功能,使用前请调用SoundUtil.init();
     */
    public static SetAllSound(){
        let key = ReadLocal('SoundSwitch', 'true');
        if (key == 'true') {
            SaveLocal('SoundSwitch', 'false');
            SoundUtil.musicKey = false;
            SoundUtil.stopMusic();
            // SoundUtil.stopAllEffect();
            cc.audioEngine.stopAllEffects();
            SaveLocal('EffectSwitch', 'false');
            SoundUtil.effectKey = false;
        } else {
            SaveLocal('SoundSwitch', 'true');
            SoundUtil.musicKey = true;
            SoundUtil.playMusic(null);

            SaveLocal('EffectSwitch', 'true');
            SoundUtil.effectKey = true;

            let pauseEffect = Object.keys(SoundUtil.loopEffect);
            SoundUtil.loopEffect = {};
            for(let name of pauseEffect){
                SoundUtil.playEffect(name,true);
            }

        }
    }
    static lastExpBack = {};
    static lastExplainMusic = null;
    public static playExpEffect(fileName,back?){
        if (SoundUtil.effectKey == null){
            SoundUtil.init();
        }
        if (SoundUtil.effectKey == false) {
            return;
        }
        if (SoundUtil.lastExplainMusic!=null){
            SoundUtil.stopExpEffect();
        }
        let url = 'Sound/' + fileName;
        cc.loader.loadRes(url, (err, res) => {
            if (err) {
                Log(err.message || err);
                return;
            }
            Log('播放标签音效:',fileName);
            //如果是循环播放的音效需要做重复处理
            let audioId = cc.audioEngine.playEffect(res, false);
            SoundUtil.lastExplainMusic = audioId;
            if (back){
                SoundUtil.lastExpBack[audioId] = back;
            }
        });
    }
    public static stopExpEffect(){
        Log('停止播放:',SoundUtil.lastExplainMusic)
        cc.audioEngine.stopEffect(SoundUtil.lastExplainMusic);
        if (SoundUtil.lastExpBack[SoundUtil.lastExplainMusic]){
            SoundUtil.lastExpBack[SoundUtil.lastExplainMusic]();
            SoundUtil.lastExpBack[SoundUtil.lastExplainMusic] = null;
        }
    }
}